package DataStructure.list.listRealize;

import Common.Constant.C;
import Common.Utils.UTFactory;
import DataStructure.list.Nodelj;
import Top100.DoublePoint;
import Top100.OperaTheKth;
import org.junit.Test;

/**
 * @author 蔚蔚樱
 * @version 1.0
 * @date 2021/1/6
 * @author—Email micromicrohard@outlook.com
 * @blogURL https://blog.csdn.net/Micro_Micro_Hard
 * @description 链表的倒数第K个元素
 * 1、使用双指针
 * 2、递归
 */

public class TheLastKOfLink_Recursion implements TheLastKOfLink, OperaTheKth, DoublePoint {

    @Test // 验证功能：从数据库获取测试数据，用于单元测试
    public void TestFunc() throws Exception {
        UTFactory.FullTest(this.getClass());
    }

    @Test // 调试功能 : 用于复现错误的测试案例
    public void DoubleTrack() throws Exception {
        String input = "{7, 3, 8, 5, 9, 7, 6, 5, 9}  ############  5";
        String output = "9";
        UTFactory.DebugTest(this.getClass(), input, output);
    }

    Nodelj findNode = null;

    public int findMethod(Nodelj root, int lastK) {
        findNode = null;
        if (root == null || lastK <= 0) {
            return C.ErrorNum;
        }
        roll(root, lastK);
        if (findNode == null) {
            return C.ErrorNum;
        }
        return findNode.key;
    }

    public int roll(Nodelj root, int lastK) {
        if (root.next == null) {
            return 1;
        }
        int lastLevel = roll(root.next, lastK) + 1;
        if (lastLevel == lastK) {
            findNode = new Nodelj(); // attention : 不进行初始化，会产生空指针
            findNode.key = root.key;
        }
        return lastLevel;
    }

    public Nodelj deleteTheLastK(Nodelj first, int lastK) {
        return null;
    }
}
